package com.gopay.solution.manager.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gopay.solution.dao.SolutionRuleDtlDAO;
import com.gopay.solution.dao.SsCapSolutionRelDAO;
import com.gopay.solution.domain.po.SolutionRuleDtl;
import com.gopay.solution.domain.po.SsCapSolutionRel;
import com.gopay.solution.domain.vo.Solution;
import com.gopay.solution.manager.SolutionManager;
import com.gopay.solution.manager.SsCapSolutionManager;

/**
 * 客户产品与方案关系
 * 
 * @SsApSolutionRelManager.java
 * @author caoyongfeng
 * @2013-8-1 下午2:08:39 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("ssCapSolutionManager")
public class SsCapSolutionManagerImpl implements SsCapSolutionManager {
	/**
	 * 客户产品与方案关系
	 */
	@Resource(name = "ssCapSolutionRelDAO")
	private SsCapSolutionRelDAO ssCapSolutionRelDAO;

	@Resource(name = "ssTransLimitConfSoluManager")
	private SolutionManager ssTransLimitConfSoluManager;

	/**
	 * 查询客户产品特定方案关系
	 * 
	 * @param acctProdCode
	 * @param solutionType
	 * @return
	 */
	public List<SsCapSolutionRel> findRelByCustId(String custId,
			String solutionType) {
		String hql = "from SsCapSolutionRel where custId=:custId and solutionType=:solutionType";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("custId", custId);
		param.put("solutionType", solutionType);
		// 产寻账户产品与方案关系表(solutionType=TRN)
		List<SsCapSolutionRel> ssCapSolutionRels = ssCapSolutionRelDAO
				.findByHql(hql, param);
		if (ssCapSolutionRels == null || ssCapSolutionRels.isEmpty()) {
			return null;
		}
		return ssCapSolutionRels;
	}

	/**
	 * 查询客户产品特定方案关系
	 * 
	 * @param acctProdCode
	 * @param solutionType
	 * @return
	 */
	public SsCapSolutionRel findRelByCustId(String custId, String prodCode,
			String solutionType) {
		String hql = "from SsCapSolutionRel where custId=:custId and solutionType=:solutionType and prodCode=:prodCode";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("custId", custId);
		param.put("prodCode", prodCode);
		param.put("solutionType", solutionType);
		// 产寻账户产品与方案关系表(solutionType=TRN)
		List<SsCapSolutionRel> ssCapSolutionRels = ssCapSolutionRelDAO
				.findByHql(hql, param);
		if (ssCapSolutionRels == null || ssCapSolutionRels.isEmpty()) {
			return null;
		} else {
			return ssCapSolutionRels.get(0);
		}
	}

	public SsCapSolutionRelDAO getSsCapSolutionRelDAO() {
		return ssCapSolutionRelDAO;
	}

	public void setSsCapSolutionRelDAO(SsCapSolutionRelDAO ssCapSolutionRelDAO) {
		this.ssCapSolutionRelDAO = ssCapSolutionRelDAO;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {
			Exception.class, RuntimeException.class })
	public SsCapSolutionRel saveRelAndChild(SsCapSolutionRel rel, Solution s) {
		ssTransLimitConfSoluManager.save(s);
		rel.setSolutionId(s.getMeta().getSolutionId());
		ssCapSolutionRelDAO.save(rel);
		return rel;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {
			Exception.class, RuntimeException.class })
	public void updateSolutionChild(Solution s, String solutionType) {
		ssTransLimitConfSoluManager.modify(s);
	}

	@Override
	public void deleteRelAndChild(SsCapSolutionRel rel, String solutionType) {
		ssTransLimitConfSoluManager.delete(rel.getSolutionId());
	}

}
